from typing import Callable
from fastapi import FastAPI
from routers.database.redis import redis_pool

def startup(app: FastAPI) -> Callable:
    """
    FastApi 启动完成事件
    :param app: FastAPI
    :return: start_app
    """
    async def app_start() -> None:
        # APP启动完成后触发
        print("fastapi已启动")
        # 注入缓存到app state
        app.state.redis = await redis_pool()
    return app_start


def stopping(app: FastAPI) -> Callable:
    """
    FastApi 停止事件
    :param app: FastAPI
    :return: stop_app
    """
    async def stop_app() -> None:
        # APP停止时触发
        print("fastapi已停止")
        if app.state.redis:
            await app.state.redis.close()
            # await app.state.redis.wait_closed()
    return stop_app
